package com.globallogic.vacationplanner.web.controller;

import java.io.Serializable;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

import org.apache.log4j.Logger;

import com.globallogic.vacationplanner.transfer.dto.impl.ProjectDto;
import com.globallogic.vacationplanner.transfer.dto.impl.UserDto;
import com.globallogic.vacationplanner.transfer.exception.DaoException;
import com.globallogic.vacationplanner.transfer.service.IUserService;
import com.globallogic.vacationplanner.web.exceptions.ViewException;
import com.globallogic.vacationplanner.web.helper.UserBeanHelper;

@ManagedBean
@SessionScoped
public class UserBean extends BaseBean implements Serializable {
	private static final long serialVersionUID = 1L;
	private static Logger LOGGER = Logger.getLogger(UserBean.class);
	private static final String AUTHORIZE_LOGIN_ERROR_MESSAGE = "Incorrect login or password";
	private static final String REGISTRATION_LOGIN_ERROR_MESSAGE = "User with this login already exist";
	private static final String PROFILE_CHANGED_SUCCESSFUL = "Profile has been changed";
	private static final String PASSWORD_DOES_NOT_MATCH = "Passwords does not match";

	private UserDto userDto = new UserDto();
	private IUserService userService;
	private String loginError = null;
	private String changeProfileMessage = null;
	private String registrationMessage = null;

	/**
	 * Register user;
	 * 
	 * @return string
	 */
	public String register() {
		LOGGER.info("start to register user");
		try {
			userDto = new UserBeanHelper().buildUserDto(this);
		} catch (ViewException e1) {
			registrationMessage = PASSWORD_DOES_NOT_MATCH;
			return "registrationFailed";
		}
		userService = getServiceContext().getUserService();
		try {
			if (userService.register(userDto)) {
				LOGGER.info("register new user complete successful");
				LOGGER.error(loginError);
				return "registrationSuccess";
			}
		} catch (DaoException e) {
			LOGGER.error(loginError);
			loginError = REGISTRATION_LOGIN_ERROR_MESSAGE;
		}
		return "registrationFailed";
	}

	/**
	 * Authorize user.
	 * 
	 * @return string
	 */
	public String authorize() {
		LOGGER.info("Start authorize user");
		try {
			userDto = new UserBeanHelper().buildUserDto(this);
		} catch (ViewException e1) {
			loginError = AUTHORIZE_LOGIN_ERROR_MESSAGE;
		}
		userService = getServiceContext().getUserService();
		if (userService.authorize(userDto)) {
			LOGGER.info(" authorize user success");
			return "authorizationSuccess";
		}
		loginError = AUTHORIZE_LOGIN_ERROR_MESSAGE;
		return "authorizationFailed";
	}

	/**
	 * Change user profile information.
	 * 
	 * @return string
	 */
	public String changeProfile() {
		LOGGER.info("start to change user profile ");
		try {
			userDto = new UserBeanHelper().buildUserDto(this);
		} catch (ViewException e1) {
			changeProfileMessage = PASSWORD_DOES_NOT_MATCH;
		}
		userService = getServiceContext().getUserService();
		try {
			userService.changeProfile(userDto);
			changeProfileMessage = PROFILE_CHANGED_SUCCESSFUL;
			LOGGER.info(changeProfileMessage);
		} catch (DaoException e) {
			changeProfileMessage = PASSWORD_DOES_NOT_MATCH;
			LOGGER.error(changeProfileMessage);
		}
		return "changeProfile";
	}

	public String logout() {
		FacesContext.getCurrentInstance().getExternalContext()
				.invalidateSession();
		return "logout";
	}

	public String getChangeProfileMessage() {
		return changeProfileMessage;
	}

	public void setChangeProfileMessage(String changeProfileMessage) {
		this.changeProfileMessage = changeProfileMessage;
	}

	public void setLogin(String login) {
		userDto.setLogin(login);
	}

	public void setPassword(String password) {
		userDto.setPassword(password);
	}

	public void setConfirmPassword(String confirmPassword) {
		userDto.setConfirmPassword(confirmPassword);
	}

	public void setUserName(String username) {
		userDto.setUserName(username);
	}

	public void setSurname(String surname) {
		userDto.setSurname(surname);
	}

	public void setEmail(String email) {
		userDto.setEmail(email);
	}

	public void setProjectDto(List<ProjectDto> projectDto) {
		userDto.setProjectDto(projectDto);
	}

	public String getLoginError() {
		return loginError;
	}

	public void setLoginError(String loginError) {
		this.loginError = loginError;
	}

	public String getLogin() {
		return userDto.getLogin();
	}

	public String getPassword() {
		return userDto.getPassword();
	}

	public String getConfirmPassword() {
		return userDto.getConfirmPassword();
	}

	public String getUserName() {
		return userDto.getUserName();
	}

	public String getSurname() {
		return userDto.getSurname();
	}

	public String getEmail() {
		return userDto.getEmail();
	}

	public List<ProjectDto> getList() {
		return userDto.getProjectDto();
	}

	public String getRegistrationMessage() {
		return registrationMessage;
	}

	public void setRegistrationMessage(String registrationMessage) {
		this.registrationMessage = registrationMessage;
	}

}
